package com.cskaoyan;

import com.cskaoyan.mapper.ClazzMapper;
import com.cskaoyan.mapper.CommitMapper;
import com.cskaoyan.mapper.User2Mapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.vo.Clazz;
import com.cskaoyan.vo.Commit;
import com.cskaoyan.vo.User;
import com.cskaoyan.vo.User2;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class Main {

    static SqlSession sqlSession;
    static SqlSession sqlSession2;

    static User2Mapper user2Mapper;
    static CommitMapper commitMapper;


    @BeforeClass
    public static void init(){

        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        try {
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsReader("mybatis-config.xml"));

            sqlSession = sqlSessionFactory.openSession();
            sqlSession2 = sqlSessionFactory.openSession();
            user2Mapper = sqlSession.getMapper(User2Mapper.class);
            commitMapper = sqlSession.getMapper(CommitMapper.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @AfterClass
    public static void destory() {

        sqlSession.commit();
        sqlSession.close();

    }

    @Test
    public void test01(){

        List<Object> userList = sqlSession.selectList("com.cskaoyan.mapper.UserMapper.selectAll");

        System.out.println(userList);

    }


    @Test
    public void testProxy() {


        UserMapper mapper = sqlSession.getMapper(UserMapper.class);


//        List<User> users = mapper.selectAll();

//        System.out.println(users);

    }

    @Test
    public void testSelectClazz(){

        ClazzMapper mapper = sqlSession.getMapper(ClazzMapper.class);

        Clazz clazz = mapper.selectClazzById(1);

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(clazz);

    }

//    @Test
//    public void testProxy2() {
//
//
//        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//        UserMapper mapper1 = sqlSession2.getMapper(UserMapper.class);
//
//        List<User> users = mapper.selectAll();
//        sqlSession.commit();
////        sqlSession.close();
//
//        List<User> users1 = mapper1.selectAll();
//
//
//        System.out.println(users);
//        System.out.println(users1);
//
//    }


    @Test
    public void testUpdateCommit(){

        // 获取所有的user
        List<User2> user2List = user2Mapper.selectAllUser2();

        // 获取所有的配置
        List<Commit> commitList = commitMapper.getAllCommit();

        System.out.println(user2List);
        System.out.println(commitList);


        for (Commit commit : commitList) {

            // 获取到张三、李四
            String userId = commit.getUserId();

            for (User2 user2 : user2List) {
                String name = user2.getName();
                if (name.equals(userId)) {
                    Integer id = user2.getId();
                    commit.setUserId(String.valueOf(id));
                    break;
                }
            }
        }

        int i = 0;



    }


}
